<?php
/**
 * file video.php
 * Created on 2015-3-21 下午6:04:49 
 * Create by xiweijie
 */
namespace haibao\cms\business;

use \haibao\cms\data\VideoCategory as VideoCategoryData;
use \haibao\frame\data\query\Condition;
class VideoCategory extends \haibao\cms\business\Base{
    
    public static function addVideoCategory($model){
        self::checkOperation(self::FUNC_CODE_VIDEO_CATEGORY_ADD);
        if(empty($model)){
            throw new BusinessException('分类实体不能为空',self::DATA_EMPTY);
        }
        if(empty($model->CategoryName)){
            throw new BusinessException('分类名称不能为空',self::DATA_EMPTY);
        }
        if(empty($model->PicId)){
            throw new BusinessException('清上传图片',self::DATA_EMPTY);
        }
        if(!self::categoryNameIsSet($model->CategoryName)){
            throw new BusinessException('类别名称已存在！');
        }
        $model->Status = \haibao\cms\model\data\VideoCategory::STATUS_ENABLE;
        
        $videoCategoryData = new VideoCategoryData();
        $model =  $videoCategoryData->addVideoCategory($model);
        self::saveOperateHistory(self::FUNC_CODE_VIDEO_CATEGORY_ADD, $model->toArray(true,true));
        return $model;
    }
    
    public static function updateVideoCategory($model){
        self::checkOperation(self::FUNC_CODE_VIDEO_CATEGORY_EDIT);
        if(empty($model)){
            throw new BusinessException('类型不能为空',self::DATA_EMPTY);
        }
        if(!$model->CategoryName){
            throw new BusinessException('类型名称不能为空',self::DATA_EMPTY);
        }
        if(!$model->PicId){
            throw new BusinessException('类型图片不能为空',self::DATA_EMPTY);
        }
        if(!self::categoryNameIsSet($model->CategoryName,$model->Id)){
            throw new BusinessException('类别名称已存在！');
        }
        $videoCategoryData = new VideoCategoryData();
        $videoCategoryData->updateVideoCategoryById($model);
        self::saveOperateHistory(self::FUNC_CODE_VIDEO_CATEGORY_EDIT, $model->toArray(true,true));
    } 
    
    public static function delVideoCategory($categoryId){
        self::checkOperation(self::FUNC_CODE_VIDEO_CATEGORY_DEL);
        if(!$categoryId){
            throw new BusinessException('类别Id不能为空',self::DATA_EMPTY_ID);
        }
        if(self::isExitstChildCategory($categoryId)){
            throw new BusinessException('当前分类下有子分类不能删除');
        }
        $videoCategoryData = new VideoCategoryData();
        $videoCategoryData->deleteVideoCategoryById($categoryId);
        self::saveOperateHistory(self::FUNC_CODE_VIDEO_CATEGORY_DEL, array('Id' =>$categoryId));
    }
    
    public static function getCategoryTree(){
        $videoCategoryData = new VideoCategoryData();
        return $videoCategoryData->getCategoryTree();
    }

    public static function getAllCategory(){
        $data = new VideoCategoryData();
        return $data->getAllCategoryList();
    }
    
    public static function categoryNameIsSet($categoryName,$categoryId=null){
        $categoryFilter = new \haibao\cms\model\filter\VideoCategory();
        if($categoryId){
            $categoryFilter->where(\haibao\cms\model\filter\VideoCategory::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTEQUAL, $categoryId);
        }
        $categoryFilter->where(\haibao\cms\model\filter\VideoCategory::CONDITION_FIELD_NAME_CATEGORYNAME, Condition::CONDITION_EQUAL, $categoryName);
        $data = new VideoCategoryData();
        if($data->getAll($categoryFilter)){
            return false;
        }else{
            return true;
        }
    }
    
    public static function getCategoryById($categoryId){
        if(!$categoryId){
            throw new BusinessException('分类id不能为空',Base::DATA_EMPTY_ID);
        }
        $categoryData = new VideoCategoryData();
        return $categoryData->getCategoryById($categoryId);
    }
    
    public static function isExitstChildCategory($id){
        $categoryData = new VideoCategoryData();
        $count = $categoryData->isExitstChildCategory($id);
        return $count > 0 ? true : false;
    }
    
    public static function searchCategories($filter){
    	$categoryData = new VideoCategoryData();
    	
    	return $categoryData->getAll($filter);
    }
}